package com.ck.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.ck.Service;
import com.ck.entity.Worker;
import com.ck.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@RestController
@RequestMapping
public class TestController {

    @Autowired
    private Service service;
    @Autowired
    private CommodityService commodityService;

    private static final ReentrantLock LOCK = new ReentrantLock();

    private static final Log LOG = LogFactory.get();

    /**
     *
     * @param name
     * @return
     */
    @GetMapping("/hello")
    public String say(@RequestParam String name){
        return "1111";
    }

    @PostMapping("/save")
    public String save(@RequestBody String body,@RequestHeader String header){
        return body + "==="+header;
    }

    @GetMapping("/saveWork")
    public Integer saveWork(@RequestParam Integer id) throws Exception {
        Worker worker = new Worker();
        worker.setId(id);
        return commodityService.saveWork(worker);
    }

    @RequestMapping("/thread")
    public String threadSave(){
        for(int index =0; index < 5;index++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try{
                        Worker worker = new Worker();
                        worker.setId(21);
                        int res = commodityService.saveWork(worker);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            },index+"").start();
        }
        return "success";
    }
}
